package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.ProjectPlans;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;
    /**
     *
     *2.1 查询所有护理计划
     */
    @Override
    public List<NursingPlanVo> getPlanAll() {
        List<NursingPlan> nursingPlans = nursingPlanMapper.selectAll();
        List<NursingPlanVo> nursingPlanVos = nursingPlans.stream().map(
                nursingPlan -> {
                    NursingPlanVo nursingPlanVo = new NursingPlanVo();
                    BeanUtils.copyProperties(nursingPlan, nursingPlanVo);
                    return nursingPlanVo;
                }).collect(Collectors.toList());
        return nursingPlanVos;
    }

    /**
     * 新增护理计划
     *
     *
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addPlan(NursingPlanDto nursingPlanDto) {
        NursingPlan bean = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        // 插入计划表
        bean.setCreateTime(LocalDateTime.now());
        bean.setUpdateTime(LocalDateTime.now());
        nursingPlanMapper.insert(bean);

        // 获取到将要添加的护理计划项目关联实体
        List<ProjectPlans> projectPlan = nursingPlanDto.getProjectPlans().stream().peek((e) -> {
            e.setCreateTime(LocalDateTime.now());
            e.setUpdateTime(LocalDateTime.now());
            e.setPlanId(bean.getId().intValue());
        }).collect(Collectors.toList());
        // 插入关联表
        nursingProjectPlanMapper.addNursingProjectPlan(projectPlan);
    }

    /**
     *
     * 条件查询
     */
    @Override
    public PageResponse selectPage(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum,pageSize);
        Page<NursingPlanVo> nursingPlanVos = nursingPlanMapper.selectPlan(name, status);
        PageResponse<NursingPlanVo> nursingPlanVoPageResponse = PageResponse.of(nursingPlanVos, NursingPlanVo.class);
        return nursingPlanVoPageResponse;
    }

    /**
     * 根据id查询
     *
     *
     */
    @Override
    public NursingPlanVo getByiD(Long id) {
        NursingPlan nursingPlan = nursingPlanMapper.selectById(id);
        NursingPlanVo nursingPlanVo = BeanUtil.toBean(nursingPlan, NursingPlanVo.class);
        return nursingPlanVo;
    }

    /**
     * 修改计划
     *
     *
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public void update(NursingPlanDto nursingPlanDto) {

        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingProjectPlanMapper.deleteNursingProjectPlan(nursingPlan.getId());
        // 插入计划表
        nursingPlan.setCreateTime(LocalDateTime.now());
        nursingPlan.setUpdateTime(LocalDateTime.now());
        nursingPlanMapper.update(nursingPlan);


        // 获取到将要添加的护理计划项目关联实体
        List<ProjectPlans> projectPlans = nursingPlanDto.getProjectPlans().stream().peek((e) -> {
            e.setCreateTime(LocalDateTime.now());
            e.setUpdateTime(LocalDateTime.now());
            e.setPlanId(nursingPlan.getId().intValue());
        }).collect(Collectors.toList());
        // 插入关联表
        nursingProjectPlanMapper.addNursingProjectPlan(projectPlans);
    }


    /**
     *
     *删除计划
     *
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(Long id) {
        NursingPlan nursingPlan = nursingPlanMapper.selectById(id);
        if (ObjectUtil.isNotEmpty(nursingPlan) && nursingPlan.getStatus().equals(1)) {
            throw new BaseException(BasicEnum.ENABLED_CANNOT_DELETED);
        }
        nursingProjectPlanMapper.deleteNursingProjectPlan(id);
        nursingPlanMapper.deleteById(id);
    }


    /**
     * 启用禁用
     *
     *
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        nursingPlanMapper.updateStatus(id,status);
    }


}
